package com.stylefeng.guns.rest.modular.film.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.huangliusong.api.film.FilmServiceApi;
import com.huangliusong.api.film.VO.*;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.List;

/**
 * @author huangliusong
 * @since 2019/1/12
 * {@link }
 */
@Component
@Service
public class DefaultFilmServiceImpl implements FilmServiceApi {

    @Autowired(required = false)
    private MoocBannerTMapper moocBannerTMapper;

    @Autowired(required = false)
    private MoocFilmTMapper moocFilmTMapper;


    @Autowired(required = false)
    private MoocCatDictTMapper moocCatDictTMapper;

    @Autowired(required = false)
    private MoocVearDictTMapper moocYearDictTMapper;


    @Autowired(required = false)
    private MoocSourceDictTMapper  moocSourceDictTMapper;




    @Override
    public List<BannerVO> getBanner() {
        List<BannerVO> result=new ArrayList<>();
        List<MoocBannerT> moocBanners=moocBannerTMapper.selectList(null);
        for(MoocBannerT moocBannerT:moocBanners){
            BannerVO bannerVO=new BannerVO();
            bannerVO.setBannerId(moocBannerT.getUuid()+"");
            bannerVO.setBannerUrl(moocBannerT.getBannerUrl());
            bannerVO.setBannerAddress(moocBannerT.getBannerAddress());
            result.add(bannerVO);
        }
        return result;
    }


    private List<FilmInfo> getFilmInfos(List<MoocFilmT> moocFilmTS){
        List<FilmInfo> filmInfos=new ArrayList<>();
        for (MoocFilmT filmT:moocFilmTS){
            FilmInfo filmInfo=new FilmInfo();
            filmInfo.setScore(filmT.getFileScore());
            filmInfo.setImgAddress(filmT.getImgAddress());
            filmInfo.setFilmType(filmT.getFileType());
            filmInfo.setFilmScore(filmT.getFileScore());
            filmInfo.setFilmName(filmT.getFileName());
            filmInfo.setFilmId(filmT.getUuid()+"");
            filmInfo.setExpectNum(filmT.getFilePresalenum());
            filmInfo.setBoxNum(filmT.getFileBoxOffice());
            filmInfo.setShowTime(DateUtil.getDay(filmT.getFileTime()));
            //将转换的对象放入filminfos
            filmInfos.add(filmInfo);
        }
        return filmInfos;
    }
    @Override
    public FilmVO getHotFilms(boolean isLimit, int nums) {
        //判断是会否是首页需要的内容
        FilmVO filmVO=new FilmVO();
        List<FilmInfo> filmInfos=new ArrayList<>();
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("file_status","1");
        if (isLimit){
            //
            Page<MoocFilmT> page=new Page<>(1,nums);
            List<MoocFilmT> moocFilmTS=moocFilmTMapper.selectPage(page,entityWrapper);
            //组织fileinfos
            filmInfos= getFilmInfos(moocFilmTS);
            filmVO.setFileNum(moocFilmTS.size());
            filmVO.setFilmInfos(filmInfos);
        }else {

        }
        return filmVO;
    }

    @Override
    public FilmVO getSoonFilms(boolean isLimit, int nums) {
        //判断是会否是首页需要的内容
        FilmVO filmVO=new FilmVO();
        List<FilmInfo> filmInfos=new ArrayList<>();
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("file_status","2");
        if (isLimit){
            //
            Page<MoocFilmT> page=new Page<>(1,nums);
            System.out.println("getSoonFilms");
            List<MoocFilmT> moocFilmTS=moocFilmTMapper.selectPage(page,entityWrapper);
            //组织fileinfos
            filmInfos= getFilmInfos(moocFilmTS);
            filmVO.setFileNum(moocFilmTS.size());
            filmVO.setFilmInfos(filmInfos);
        }else {

        }
        return filmVO;
    }

    @Override
    public List<FilmInfo> getBoxRanking() {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("file_status","1");

        Page<MoocFilmT> page=new Page<>(1,10,"file_box_office");
        List<MoocFilmT> moocFilms=moocFilmTMapper.selectPage(page,entityWrapper);
        List<FilmInfo> filmInfos=getFilmInfos(moocFilms);
        return filmInfos;
    }

    @Override
    public List<FilmInfo> getExpectRange() {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("file_status","2");
        Page<MoocFilmT> page=new Page<>(1,10,"file_source");
        List<MoocFilmT> moocFilms=moocFilmTMapper.selectPage(page,entityWrapper);
        List<FilmInfo> filmInfos=getFilmInfos(moocFilms);
        return filmInfos;
    }

    @Override
    public List<FilmInfo> getTop() {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("file_status","1");
        Page<MoocFilmT> page=new Page<>(1,10,"file_score");
        List<MoocFilmT> moocFilms=moocFilmTMapper.selectPage(page,entityWrapper);
        List<FilmInfo> filmInfos=getFilmInfos(moocFilms);
        return filmInfos;
    }

    @Override
    public List<CatVO> getCats() {
        List<CatVO> cats=new ArrayList<>();
        List<MoocCatDictT> moocCats=moocCatDictTMapper.selectList(null);
        for (MoocCatDictT moocCatDictT:moocCats){
            CatVO catVO=new CatVO();
            catVO.setCatId(moocCatDictT.getUuid()+"");
            catVO.setCatName(moocCatDictT.getShowName());
            cats.add(catVO);
        }
        return cats;
    }

    @Override
    public List<SourceVO> getSource() {
        List<SourceVO> sources=new ArrayList<>();
        List<MoocSourceDictT> moocSourceDictTS=moocSourceDictTMapper.selectList(null);
        for (MoocSourceDictT moocSourceDictT:moocSourceDictTS){
            SourceVO sourceVO=new SourceVO();
            sourceVO.setSourceId(moocSourceDictT.getUuid()+"");
            sourceVO.setSourceName(moocSourceDictT.getShowName());
            sources.add(sourceVO);
        }
        return sources;
    }

    @Override
    public List<YearVo> getYear() {
        List<YearVo> years=new ArrayList<>();
        List<MoocVearDictT> moocYearDictTS=moocYearDictTMapper.selectList(null);
        for (MoocVearDictT moocYearDict:moocYearDictTS){
            YearVo yearVo=new YearVo();
            yearVo.setYearId(moocYearDict.getUuid()+"");
            yearVo.setYearName(moocYearDict.getShowName());
            years.add(yearVo);
        }
        return years;
    }
}
